class Solution {
public:
    int getmid(vector<int>& a, int l, int r) {
        int num = a[l];
        while(l < r) {
            while(l < r && num >= a[r]) {
                r--;
            }
            a[l] = a[r];
            while(l < r && num <= a[l]) {
                l++;
            }
            a[r] = a[l];
        }
        a[l] = num;
        return l;
    }

    void quicksort(vector<int>& a, int l, int r) {
        if(l < r) {
            int mid = getmid(a, l, r);
            quicksort(a, l, mid- 1);
            quicksort(a, mid + 1, r);
        }
    }

    int findKth(vector<int> a, int n, int K) {
        // write code here
        quicksort(a, 0, n - 1);

        return a[K - 1];
    }
};